package logic.newLogic2;

import java.security.NoSuchAlgorithmException;
import java.util.TreeSet;

import logic.Encriptor;
import logic.Logger;

import guiDisplay.RecommendationList;
import data.RatingTuple;
import data.SystemData;
import data.UserTuple;
import exceptions.MissingMandantoryFieldException;
import exceptions.UserAlreadyExistException;
import exceptions.WrongPasswordException;

public class UserHandler3 {

	public UserHandler3(){

	}

	public boolean login(String userName, String password){
		System.out.println(userName);
		UserTuple userT= SystemData.getInstance().getUser(userName);
		if (userT == null){
				return false;
		}
		if (userT.isPassword(password)){
			Logger.getInstance().userLogIn(userT);
			return true;
		}
		return false;
	}

	public void logout(/*String userName*/) {

	}

	//0 - ok
	//1 - passwords dont match
	//2 - missing mandatory field
	//3 - user already exits
	//4 - SQL error
	public int registerUser(String userName, String password1,
		String password2, String day, String month, String year, String sex) throws WrongPasswordException, MissingMandantoryFieldException, UserAlreadyExistException{
		if (sex.equals("Female"))
			sex="true";
		else sex="false";
		if (!(password2.equals(password1))){
			return 1;
		}
		else if (userName.equals("")||(day.equals(""))||(year.equals(""))||(month.equals("")) || (password1.equals("")) || (password2.equals(""))){
			return 2;
		}
		else{
			if (SystemData.getInstance().getUser(userName)!=null){
				return 3;
			}else{
			//	user.encryptPassword();
				UserTuple user = SystemData.getInstance().createUser(userName, password1, sex,day+"/" + month +"/"+ year, "user");
				if (user!=null){
					return 0;
				} else {
					return 4;
				}
			}
		}

	}

	//0 -ok
	//1 -wrong pass
	//2 -passwords dont match
	public int changeDetails(String name, String oldpass, String pass1, String pass2,
			String day, String month, String year, String sex) {
		UserTuple user = SystemData.getInstance().getUser(name);
		if ("male".equals(sex))
			sex="false";
		else sex="true";
		SystemData.getInstance().editUser(name,day+"/" + month +"/"+ year, sex);
		if (oldpass.equals("")&& pass1.equals("")&& pass2.equals("")){
			SystemData.getInstance().editUser(name,day+"/" + month +"/"+ year, sex);
			return 0;
		}

		if (!user.isPassword(oldpass)){
			return 1;
		}else if((!("".equals(pass1)) || !("".equals(pass2)))  && !pass1.equals(pass2)){
			return 2;
		}else{
			//TODO check that new details are not null
			try {
				String pass=Encriptor.encryptUserPassword(pass1);
				SystemData.getInstance().editUser(name,day+"/" + month +"/"+ year, sex);
				SystemData.getInstance().changeUserPassword(name, pass);
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}

			return 0;
		}
	}

	public RecommendationList getMovies(String user, int begin) {
		return SystemData.getInstance().getUser(user).getRecs(begin);

	}

	public boolean hasSeenMovie(String movieName, String userName){
		SystemData sd = SystemData.getInstance();
		if (sd.getRating(userName, movieName)==null){
			return false;
		}
		else{
			return true;
		}
	}

	public int getNumRating(String user) {
		return SystemData.getInstance().getRatingsByUser(user).size();
	}

	public TreeSet<RatingTuple> getUserRatings(String user) {
		return SystemData.getInstance().getUser(user).getRatings();
	}


	public String[] haveSeenRating(String user, String movie) {
		RatingTuple rating = SystemData.getInstance().getRating(user, movie);
		String ans[] = {null, null};
		if (rating != null){
			ans[0] = ""+rating.get_rating()+"";
			ans[1] = rating.get_description();
		}
		return ans;
	}

	public void setAdmin(String name){
		SystemData.getInstance().setAdmin(name);
	}

	public void setManager(String name){
		SystemData.getInstance().setManager(name);
	}

	public boolean isAdmin(String user) {
		return SystemData.getInstance().getUser(user).isAdmin();
	}

	public boolean isManager(String user) {
		return SystemData.getInstance().getUser(user).isManager();
	}
}
